<?php
class ThanhtichsvsController extends AppController {

	var $name = 'Thanhtichsvs';
    var $uses= array('Thanhtichsv','Hockie','Tgiachidoan');
	
    function index($phongtrao_id=null)
    {
    	if(!$phongtrao_id)
    		$this->redirect(array('controller'=>'pages','action'=>'home'));
    	$phongtrao = $this->Thanhtichsv->Phongtraosv->find('first',array('recursive'=>-1,'conditions'=>array('Phongtraosv.id'=>$phongtrao_id)));
    	$chidoan_id = $phongtrao['Phongtraosv']['chidoansv_id'];

    	// lay sinh vien thuoc chi doan
    	$sinhviens = $this->Tgiachidoan->find('list',array('recursive'=>'0','conditions'=>array('ngaychuyen'=>NULL,'chidoansv_id'=>$chidoan_id),'fields'=>array('Sinhvien.id','Sinhvien.hoten')));
    	
		$this->Thanhtichsv->recursive = 0;
		$this->set('thanhtichsvs', $this->paginate(array('phongtraosv_id'=>$phongtrao_id)));
		$this->set(compact('phongtrao','sinhviens'));
	}
    
	function viewtt($mssv){
        if($mssv==null){
            $this->Session->setFlash(__('Mã số sinh viên không hợp lệ', true));
        }else{
            $hocky = $this->getHocKyHt();
    		$x = $hocky['Hocky']['ngaybatdau'];
    		$y = $hocky['Hocky']['ngayketthuc'];
    		//Thanhtichsv
    		$thanhtichsvs = $this->Thanhtichsv->find('all', array('conditions' => array(
    																				'Sinhvien.id' => $mssv,
																				array('Phongtraosv.ngayketthuc BETWEEN ? AND ?' => array($x,$y))
													)));
            //pr($thanhtichs);
            $this->set(compact('thanhtichsvs'));
        }
    }
	
    function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid thanhtichsv', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('thanhtichsv', $this->Thanhtichsv->read(null, $id));
	}

	function add($phongtrao_id)
	{
		if (!empty($this->data))
		{
			$this->Thanhtichsv->create();
			
			$this->data['Thanhtichsv']['phongtraosv_id'] = $phongtrao_id;			
			if ($this->Thanhtichsv->save($this->data))
			{
				$this->Session->setFlash(__('Thông tin đã lưu', true),'success');
				$this->redirect(array('action' => 'add',$phongtrao_id));
			}
			else
			{
				$this->Session->setFlash(__('Không thể lưu vui lòng thử lại', true),'error');
			}
		}
		
		$this->Thanhtichsv->recursive = 0;
		$this->set('thanhtichsvs', $this->paginate(array('phongtraosv_id'=>$phongtrao_id)));
		$phongtrao = $this->Thanhtichsv->Phongtraosv->find('first',array('recursive'=>-1,'conditions'=>array('Phongtraosv.id'=>$phongtrao_id)));
    	$chidoan_id = $phongtrao['Phongtraosv']['chidoansv_id'];
    	$svdaco = $this->Thanhtichsv->find('list',array('conditions'=>array('phongtraosv_id'=>$phongtrao_id),'fields'=>array('sinhvien_id')));
    	$svs = $this->Tgiachidoan->find('all',array('recursive'=>'0','conditions'=>array('ngaychuyen'=>NULL,'chidoansv_id'=>$chidoan_id),'fields'=>array('Sinhvien.id','Sinhvien.mssv','Sinhvien.hoten')));//,'NOT'=>array('Sinhvien.id'=>$svdaco)
		foreach ($svs as $sv)
    	{
    		$sinhviens[$sv['Sinhvien']['id']]=$sv['Sinhvien']['mssv'].'-'.$sv['Sinhvien']['hoten'];
    	}
		$this->set(compact('sinhviens', 'phongtrao','phongtrao_id'));
	}

	function edit($id = null,$phongtrao_id)
	{
		
		if (!$id && empty($this->data))
		{
			$this->Session->setFlash(__('Giá trị không tồn tại', true));
			$this->redirect(array('action' => 'index'));
		}
		
		if (!empty($this->data))
		{
			if ($this->Thanhtichsv->save($this->data)) {
				$this->Session->setFlash(__('Thông tin đã lưu', true),'success');
				$this->redirect(array('action' => 'index',$phongtrao_id));
			} else {
				$this->Session->setFlash(__('Không thể lưu vui lòng thử lại', true),'error');
			}
		}
		
		if (empty($this->data))
		{
			$this->data = $this->Thanhtichsv->read(null, $id);
		}
		$phongtrao_id=$this->data['Thanhtichsv']['phongtraosv_id'];
		$phongtrao = $this->Thanhtichsv->Phongtraosv->find('first',array('recursive'=>-1,'conditions'=>array('Phongtraosv.id'=>$phongtrao_id)));
    	$chidoan_id = $phongtrao['Phongtraosv']['chidoansv_id'];
    	$sinhvien_id = $this->data['Thanhtichsv']['sinhvien_id'];
    	$svdaco = $this->Thanhtichsv->find('list',array('conditions'=>array('phongtraosv_id'=>$phongtrao_id,'NOT'=>array('sinhvien_id'=>$sinhvien_id)),'fields'=>array('sinhvien_id')));
    	$sinhviens = $this->Tgiachidoan->find('list',array('recursive'=>'0','conditions'=>array('ngaychuyen'=>NULL,'chidoansv_id'=>$chidoan_id),'fields'=>array('Sinhvien.id','Sinhvien.hoten')));//,'NOT'=>array('Sinhvien.id'=>$svdaco)
    	//$sinhviens = $this->Tgiachidoan->find('list',array('recursive'=>'0','conditions'=>array('chidoansv_id'=>$chidoan_id),'fields'=>array('Sinhvien.id','Sinhvien.hoten')));
		$this->set(compact('sinhviens', 'phongtrao','phongtrao_id'));
		
	}

	function delete($id = null,$phongtrao_id=null)
	{
		if(!$phongtrao_id)
			$this->redirect(array('pages','action'=>'home'));
		if (!$id)
		{
			$this->Session->setFlash(__('giá trị không tồn tại', true));
			$this->redirect(array('action'=>'index',$phongtrao_id));
		}
		if ($this->Thanhtichsv->delete($id)) {
			$this->Session->setFlash(__('Thông tin đã xóa', true),'success');
			$this->redirect(array('action'=>'index',$phongtrao_id));
		}
		$this->Session->setFlash(__('Không thể xóa. Vui lòng thử lại', true),'error');
		$this->redirect(array('action' => 'index',$phongtrao_id));
	}

	
}
